from redis import Redis
from pickle import dumps, loads


class RedisOperation:
    def __init__(self, host='localhost', port=6379, db=0, password=None):
        self.database = Redis(host, port, db, password)

    def set_data(self, key, value, ex=None):
        self.database.set(key, dumps(value), ex)

    def get_data(self, key):
        data = self.database.get(key)
        if data is None:
            return None
        return loads(data)

    def delete_key(self, key):
        self.database.delete(key)

    def ttl(self, key):
        return self.database.ttl(key)

    def get_keys(self):
        keys = self.database.keys()
        return list(map(lambda key: key.decode(), keys))


if __name__ == '__main__':
    for i in range(0, 10):
        rdm = RedisOperation(db=i)
        rdm.delete_key('name')
